﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using Mahasiswa.WP.ViewModels;

namespace Mahasiswa.WP
{
    public partial class MahasiswaDetailPage : PhoneApplicationPage
    {
        public MahasiswaDetailPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
                MahasiswaModel item = (MahasiswaModel)PhoneApplicationService.Current.State["objMahasiswa"];

                TextBlock_Title.Text = item.NIM + " - " + item.Nama;
                TextBox_Nama.Text = item.Nama;
                TextBox_Umur.Text = Convert.ToString(item.Umur);
                TextBox_Alamat.Text = item.Alamat;
                TextBox_Jurusan.Text = item.Jurusan;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public async Task UpdateMahasiswa()
        {
            try
            {
                MahasiswaModel item = (MahasiswaModel)PhoneApplicationService.Current.State["objMahasiswa"];

                MahasiswaModel mahasiswa = new MahasiswaModel();
                mahasiswa.Nama = TextBox_Nama.Text;
                mahasiswa.Alamat = TextBox_Alamat.Text;
                mahasiswa.Jurusan = TextBox_Jurusan.Text;
                mahasiswa.Umur = Convert.ToInt32(TextBox_Umur.Text);

                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.PutAsJsonAsync("http://169.254.80.80/akademik/api/Mahasiswa?nim=" + item.NIM, mahasiswa);
                response.EnsureSuccessStatusCode();

                if (response.IsSuccessStatusCode)
                {
                    MessageBox.Show("Data berhasil diupdate.");
                }
                else
                {
                    MessageBox.Show("Data gagal diupdate.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public async Task DeleteMahasiswa()
        {
            try
            {
                MahasiswaModel item = (MahasiswaModel)PhoneApplicationService.Current.State["objMahasiswa"];

                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client.DeleteAsync("http://169.254.80.80/akademik/api/Mahasiswa?nim=" + item.NIM);
                response.EnsureSuccessStatusCode();

                if (response.IsSuccessStatusCode)
                {
                    MessageBox.Show("Data berhasil dihapus.");
                }
                else
                {
                    MessageBox.Show("Data gagal dihapus.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ApplicationBarIconButton_Delete_Click(object sender, EventArgs e)
        {
            DeleteMahasiswa();
        }

        private void ApplicationBarIconButton_Update_Click(object sender, EventArgs e)
        {
            UpdateMahasiswa();
        }
    }
}